﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace blackhouse.StareKino.Web.Modules {
    public partial class OpinionCategories : System.Web.UI.UserControl {

        #region Fields

        private List<OpinionNumberUI> opinionNumbers = new List<OpinionNumberUI>();

        #endregion

        #region Properties

        public string HeaderTitle { get; set; }
        public IDictionary<string, double> Categories { get; set; }
        public string CssClass {
            get { return this.pnlSummaryOpinions.CssClass; }
            set { this.pnlSummaryOpinions.CssClass = value; }
        }

        #endregion

        #region Methods

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            this.ltlHeaderTitle.Text = this.HeaderTitle;
        }

        public void AddOpinionNumber(double number, string title) {
            this.AddOpinionNumber(number, title, String.Empty, String.Empty);
        }

        public void AddOpinionNumber(double number, string title, string format) {
            this.AddOpinionNumber(number, title, String.Empty, format);
        }

        public void AddOpinionNumber(double number, string title, string cssClass, string format) {
            OpinionNumberUI opinionNumber = new OpinionNumberUI();
            opinionNumber.Number = number;
            opinionNumber.Title = title;
            opinionNumber.CssClass = cssClass;
            opinionNumber.NumberFormat = String.IsNullOrEmpty(format) ? "F2" : format;
            this.opinionNumbers.Add(opinionNumber);
        }

        protected void rptOpinionsNumbers_ItemDataBound(object sender, RepeaterItemEventArgs e) {
            switch (e.Item.ItemType) {
                case ListItemType.AlternatingItem:
                case ListItemType.Item:
                    OpinionNumberUI opinionNumber = e.Item.DataItem as OpinionNumberUI;
                    Panel pnlSummaryOpinionsNumbers = e.Item.FindControl("pnlSummaryOpinionsNumbers") as Panel;
                    Literal ltlNumber = e.Item.FindControl("ltlNumber") as Literal;
                    Literal ltlTitle = e.Item.FindControl("ltlTitle") as Literal;
                    if (opinionNumber != null) {
                        if (pnlSummaryOpinionsNumbers != null && !String.IsNullOrEmpty(opinionNumber.CssClass)) {
                            if (!String.IsNullOrEmpty(pnlSummaryOpinionsNumbers.CssClass))
                                pnlSummaryOpinionsNumbers.CssClass += " ";
                            pnlSummaryOpinionsNumbers.CssClass += opinionNumber.CssClass;
                        }
                        if (ltlNumber != null)
                            ltlNumber.Text = opinionNumber.Number.ToString(opinionNumber.NumberFormat);
                        if (ltlTitle != null)
                            ltlTitle.Text = opinionNumber.Title;
                    }
                    break;
            }
        }

        public override void DataBind() {
            base.DataBind();
            this.rptOpinionsNumbers.DataSource = opinionNumbers;
            this.rptOpinionsNumbers.DataBind();
            if (this.Categories != null) {
                this.rptSummaryReviewCategories.DataSource = this.Categories;
                this.rptSummaryReviewCategories.DataBind();
            }
        }

        #endregion

        #region Classes

        protected class OpinionNumberUI {

            public double Number { get; set; }
            public string Title { get; set; }
            public string CssClass { get; set; }
            public string NumberFormat { get; set; }

        }

        #endregion

    }
}